package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import com.github.yulichang.base.MPJBaseServiceImpl;
import org.linlinjava.litemall.db.dao.LitemallOrderGoodsPropertyMapper;
import org.linlinjava.litemall.db.dao.LitemallOrderMapper;
import org.linlinjava.litemall.db.domain.LitemallOrder;
import org.linlinjava.litemall.db.domain.LitemallOrderGoodsProperty;
import org.linlinjava.litemall.db.domain.LitemallOrderGoodsPropertyExample;
import org.springframework.stereotype.Service;

/**
 * @author ：lilu
 */
@Service
public class LitemallOrderGoodsPropertyService extends MPJBaseServiceImpl<LitemallOrderGoodsPropertyMapper, LitemallOrderGoodsProperty> {
	
    @Resource
    private LitemallOrderGoodsPropertyMapper litemallOrderGoodsPropertyMapper;

    public LitemallOrderGoodsProperty findById(Integer id) {
        return litemallOrderGoodsPropertyMapper.selectByPrimaryKey(id);
    }

    public int add(LitemallOrderGoodsProperty record){
        record.setAddTime(LocalDateTime.now());
        return litemallOrderGoodsPropertyMapper.insertSelective(record);
    }

    public int update(LitemallOrderGoodsProperty record){
        return litemallOrderGoodsPropertyMapper.updateByPrimaryKeySelective(record);
    }


    public int deleteById(Integer id) {
        return litemallOrderGoodsPropertyMapper.deleteByPrimaryKey(id);
    }
    

    public List<LitemallOrderGoodsProperty> queryByOrderGoodsId(Integer orderGoodsId){
    	LitemallOrderGoodsPropertyExample example = new LitemallOrderGoodsPropertyExample();
    	example.or().andOrderGoodsIdEqualTo(orderGoodsId).andDeletedEqualTo(false);
    	
    	return litemallOrderGoodsPropertyMapper.selectByExample(example);
    }
    
    public int deleteByOrderId(Integer orderId){
    	LitemallOrderGoodsPropertyExample example = new LitemallOrderGoodsPropertyExample();
    	example.or().andOrderIdEqualTo(orderId).andDeletedEqualTo(false);
    	
    	return litemallOrderGoodsPropertyMapper.deleteByExample(example);
    }

    public int logicDeleteByOrderId(Integer orderId){
        LitemallOrderGoodsPropertyExample example = new LitemallOrderGoodsPropertyExample();
        example.or().andOrderIdEqualTo(orderId).andDeletedEqualTo(false);

        return litemallOrderGoodsPropertyMapper.logicalDeleteByExample(example);
    }
    
    public int deleteByOrderIdAndOrderGoodsId(Integer orderId, Integer orderGoodsId){
    	LitemallOrderGoodsPropertyExample example = new LitemallOrderGoodsPropertyExample();
    	example.or().andOrderIdEqualTo(orderId).andOrderGoodsIdEqualTo(orderGoodsId).andDeletedEqualTo(false);
    	
    	return litemallOrderGoodsPropertyMapper.deleteByExample(example);
    }
    
    public Long countByOrderGoodsId(Integer orderGoodsId){
    	LitemallOrderGoodsPropertyExample example = new LitemallOrderGoodsPropertyExample();
    	example.or().andOrderGoodsIdEqualTo(orderGoodsId).andDeletedEqualTo(false);
    	
    	return litemallOrderGoodsPropertyMapper.countByExample(example);
    }
    
    public LitemallOrderGoodsProperty findByOrderGoodsIdAndGoodsPropertyIdAndValue(Integer orderGoodsId, Integer goodsPropertyId, String value) {
        LitemallOrderGoodsPropertyExample example = new LitemallOrderGoodsPropertyExample();
        example.or().andOrderGoodsIdEqualTo(orderGoodsId).andGoodsPropertyIdEqualTo(goodsPropertyId).andValueEqualTo(value).andDeletedEqualTo(false);

        return litemallOrderGoodsPropertyMapper.selectOneByExample(example);
    }

    public LitemallOrderGoodsProperty findByOrderGoodsIdAndGoodsPropertyId(Integer orderGoodsId, Integer goodsPropertyId) {
        LitemallOrderGoodsPropertyExample example = new LitemallOrderGoodsPropertyExample();
        example.or().andOrderGoodsIdEqualTo(orderGoodsId).andGoodsPropertyIdEqualTo(goodsPropertyId).andDeletedEqualTo(false);

        return litemallOrderGoodsPropertyMapper.selectOneByExample(example);
    }

    public boolean checkPropertyIdExist(Integer propertyId) {
    	LitemallOrderGoodsPropertyExample example = new LitemallOrderGoodsPropertyExample();
    	example.or().andGoodsPropertyIdEqualTo(propertyId).andDeletedEqualTo(false);
    	return litemallOrderGoodsPropertyMapper.countByExample(example) != 0;
    }

    public List<LitemallOrderGoodsProperty> queryByOrderId(Integer orderId){
        LitemallOrderGoodsPropertyExample example = new LitemallOrderGoodsPropertyExample();
        example.or().andOrderIdEqualTo(orderId).andDeletedEqualTo(false);
        return litemallOrderGoodsPropertyMapper.selectByExample(example);
    }
}
